import com.shihan.config.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import java.io.UnsupportedEncodingException;

/**
 * @Description
 * @Author shihan
 * @Date 2020/12/26 16:58
 * @Version 1.0
 */
public class App {

    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO 基于注解方式的 IOC 容器启动，为什么要明确指明配置类，不应该是自动扫描当前类所在包下的所有类吗？
        AbstractApplicationContext context = new AnnotationConfigApplicationContext(RabbitMQConfig.class);

        // 拿到 RabbitTemplate 对象
        RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);

        // 创建 MessageProperties 对象
        MessageProperties messageProperties = MessagePropertiesBuilder.newInstance()
                .setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN)
                .setContentEncoding("UTF-8")
                .setHeader("myKey","myValue")
                .build();

        for (int i = 0; i < 10; i++) {
            // 创建 Message 对象
            Message message = new Message("你好，世界！基于注解的方式！".getBytes("utf-8"), messageProperties);

            // 发送消息：指明 Exchange 和 Routing key
            rabbitTemplate.send("exc.direct.anno.e01","rk.direct.01",message);
        }

        System.out.println("消息发送成功！");

        context.close();
    }

}
